package com.fzrskj.cloud.free.user.utils.finance;

import com.fzrskj.cloud.free.user.bean.UserAccountDetail;
import com.fzrskj.cloud.free.user.dao.UserAccountDetailDao;
import com.fzrskj.cloud.free.user.utils.FuncHandleUtils;
import com.fzrskj.cloud.free.user.utils.UserOrderUtils;
import com.fzrskj.cloud.free.user.bean.UserOrder;
import ms.core.object.CObject;

import java.util.List;

/**
 * OrderFinanceUtils.
 *
 * @author zj.
 *         Created on 2020/1/16 0016.
 */
public class OrderFinanceUtils extends CObject {
    /**
     * 订单退款功能
     */
    public boolean deleteOrder(String orderId) {
        UserOrderUtils orderUtils = new UserOrderUtils();
        UserOrder order = orderUtils.getUserOrder(orderId);
        if (null == order) {
            this.setErrCode("ORDER NOT FOUND", "订单信息不存在");
            return false;
        }
        if (!"Y".equals(order.getPayStatus())) {
            return true;
        }

        UserAccountDetailDao detailDao = new UserAccountDetailDao();
        List<UserAccountDetail> orderFinanceHistory = detailDao.getOrderHistory(orderId);
        if (orderFinanceHistory == null || orderFinanceHistory.isEmpty()) {
            this.setErrCode("ORDER FINANCE HISTORY NOT FOUND", "订单扣款记录不存在");
            return false;
        }
        FuncHandleUtils funcHandleUtils = new FuncHandleUtils();
        for (UserAccountDetail orderFinance : orderFinanceHistory) {
            //查询是否有退款记录
            UserAccountDetail done = detailDao.getOrderBackFinance(orderId, orderFinance.getUserId());
            if (null == done) {
                //如果没有，退款
                funcHandleUtils.FinanceAccount(orderFinance.getUserName(), 400, "订单作废退款", orderId, "", "", orderFinance.getAmount());
            }
        }
        return true;
    }
}
